package com.jz.community.moduleshoppingguide.nearshop.ui.view;


import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.github.florent37.viewanimator.ViewAnimator;
import com.jz.community.moduleshoppingguide.R;


public class AddWidget extends FrameLayout {

    private View sub;
    private TextView tv_count;
    private int count = 0;
    private AddButton addbutton;
    private boolean circle_anim;
    Context context;
    private boolean needSub = true;


    public interface OnAddClick {

        void onAddClick(View view, AddWidget addWidget, int count);

        void onSubClick(AddWidget addWidget, int count);

        void onNumClick();
    }

    private OnAddClick onAddClick;

    public AddWidget(@NonNull Context context) {
        super(context);
        this.context = context;
    }

    public AddWidget(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        inflate(context, R.layout.view_addwidget, this);
        addbutton = findViewById(R.id.addbutton);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AddWidget);
        for (int i = 0; i < a.getIndexCount(); i++) {
            int attr = a.getIndex(i);
            if (attr == R.styleable.AddWidget_circle_anim) {
                circle_anim = a.getBoolean(R.styleable.AddWidget_circle_anim, false);
            } else if (attr == R.styleable.AddWidget_sub_anim) {
                a.getBoolean(R.styleable.AddWidget_sub_anim, false);
            }
        }
        a.recycle();
        sub = findViewById(R.id.iv_sub);
        tv_count = findViewById(R.id.tv_count);
        addbutton.setAnimListner(() -> {
            if (onAddClick != null) {
                count++;
                onAddClick.onAddClick(addbutton, AddWidget.this, count);
            }
        });

        sub.setOnClickListener(v -> {
            if (onAddClick != null) {
                if (count > 1) {
                    if (needSub) {
                        count--;
                    }
                    onAddClick.onSubClick(AddWidget.this, count);
                } else if (count == 1) {
                    if (needSub) {
                        count--;
                        subAnim(300);
                    }
                    onAddClick.onSubClick(AddWidget.this, count);
                }
            }
        });

        tv_count.setOnClickListener(v -> {
            if (onAddClick != null) {
                onAddClick.onNumClick();
            }
        });
    }


    //减号消失动画(数字从1减到0)
    public void subAnim(int duration) {
        ViewAnimator.animate(sub)
                .translationX(0, addbutton.getLeft() - sub.getLeft())
                .rotation(-360)
                .alpha(255, 0)
                .duration(duration)
                .interpolator(new AccelerateInterpolator())
                .andAnimate(tv_count)
                .onStop(() -> {
                    if (circle_anim) {
                        addbutton.expendAnim();
                    }
                })
                .start();
        sub.setAlpha(0);
        tv_count.setAlpha(0);
    }

    //减号出现动画(数字从0加到1)
    public void addAnim(int duration) {
        ViewAnimator.animate(sub)
                .translationX(addbutton.getLeft() - sub.getLeft(), 0)
                .rotation(360)
                .alpha(0, 255)
                .duration(duration)
                .interpolator(new DecelerateInterpolator())
                .andAnimate(tv_count)
                .start();
        sub.setAlpha(1f);
        tv_count.setAlpha(1f);
        addbutton.setState(true);
    }

    public void setAddClick(OnAddClick onAddClick) {
        this.onAddClick = onAddClick;
    }

    public void setCount(int count) {
        this.count = count;
        if (this.count > 0) {
            addAnim(1);
            tv_count.setText(this.count + "");
        } else {
            tv_count.setText("");
            subAnim(1);
        }
    }

    public int getCount() {
        return count;
    }

    public void resetCount(int i) {
        if (i == 1) {
            this.count--;
        } else {
            this.count++;
        }
    }

    public void setTvText(int count) {
        if (count > 0) {
            tv_count.setText(count + "");
        } else {
            tv_count.setText("");
        }
    }


    public void setNeedSub(boolean needSub) {
        this.needSub = needSub;
    }
}
